/*
<this file provide a js modal message box without using alert() nor confirm()>
Copyright (C) <2008>  <www.sevenever.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

usage:

jsMsgBox.alert(title, message, callback);
title: title to display.
message: message to display.
callback: callback function when user click "OK" or close message box.

jsMsgBox.confirm(title, message, okcallback, cancelcallback, focusIndex)
title: title to display.
message: message to display.
okcallback: callback function when user click "OK".
cancelcallback: callback function when user click "Cancel" or close message box.
focusIndex: 1 for focus on "OK" button, 2 for focus on "Cancel" button.

bugs report to http://code.google.com/p/sevenever

*/
var jsMsgBox = {
    makeDlg:function(){
	if(this.ctnr){
	    return;
	}

	var body=document.body;

	var ctnr=document.createElement("div");
	body.appendChild(ctnr);

	var block=document.createElement("div");
	ctnr.appendChild(block);

	block.style.position="fixed";
	block.style.top="0px";
	block.style.left="0px";
	block.style.width="100%";
	block.style.height="100%";
	block.style.backgroundColor="#000";
	block.style.opacity=0.5;
	block.style.zIndex=0;


	var screen=document.createElement("div");
	ctnr.appendChild(screen);

	screen.style.position="fixed";
	screen.style.top="50%";
	screen.style.left="50%";


	var dlg=document.createElement("div");
	screen.appendChild(dlg);

	dlg.style.position="relative";
	dlg.style.top="-100px";
	dlg.style.left="-150px";
	dlg.style.width="300px";
	dlg.style.height="200px";
	dlg.style.borderWidth="1px";
	dlg.style.borderStyle="solid";
	dlg.style.borderColor="#000";
	dlg.style.backgroundColor="#FFF";
	dlg.style.textAlign="left";
	

	var title=document.createElement("div");
	dlg.appendChild(title);

	title.style.borderWidth="0px 0px 1px 0px";
	title.style.borderStyle="solid";
	title.style.borderColor="#000";
	title.style.width="100%";
	title.style.backgroundColor="blue";
	title.style.textAlign="left";
	//	title.style.maxHeight="30px";


	var xbox=document.createElement("span");
	title.appendChild(xbox);

	xbox.style.borderWidth="0px 0px 0px 1px";
	xbox.style.borderStyle="solid";
	xbox.style.backgroundColor="#CCC";
	xbox.style.display="inline";
	xbox.textContent = "X";
	xbox.style.cssFloat = "right";
	xbox.style.opacity=0.7;
	xbox.style.width=document.defaultView.getComputedStyle(xbox, null).height;
	xbox.style.textAlign="center";


	var titleText=document.createElement("div");
	title.appendChild(titleText);

	titleText.style.fontWeight="bold";
	titleText.style.maxWidth="" + (300 - document.defaultView.getComputedStyle(xbox, null).width) + "px";
	titleText.style.display="block";
	titleText.textContent="title";
	titleText.style.margin = "0px";
	titleText.style.border = "0px";
	titleText.style.padding = "0px";
	titleText.style.opacity = 0.8;
	titleText.style.overflow = "hidden";
	titleText.style.paddingLeft = "4px";
	titleText.style.color = "#FFF";


	var form=document.createElement("div");
	dlg.appendChild(form);

	form.style.margin = "0px 0px 0px 0px";
	form.style.width = "100%";
	form.style.height = "auto";
	form.style.scroll = "none";


	var msg=document.createElement("div");
	msg.style.scroll = "auto";
	msg.style.margin = "10px 10px 10px 10px";
	msg.style.height = "" + (100 - parseInt(document.defaultView.getComputedStyle(xbox, null).height) + 20) + "px";
	msg.style.overflow = "auto";
	//    msg.style.backgroundColor="#456";

	form.appendChild(msg);

	var btnArea=document.createElement("div");
	form.appendChild(btnArea);

	btnArea.style.scroll = "none";
	btnArea.style.margin = "20px 10px 10px 10px";
	btnArea.style.left = "10px";
	btnArea.style.height = "30px";
	btnArea.style.textAlign = "center";
	//    btnArea.style.backgroundColor="#654";


	var ok = document.createElement("input");
	btnArea.appendChild(ok);

	ok.type="button";
	ok.value="OK";
	ok.style.width="70px";
	ok.style.margin="0px 10px 0px 10px";
	ok.style.position="relative";
	ok.style.verticalAlign = "middle";


	var cancel = document.createElement("input");
	btnArea.appendChild(cancel);

	cancel.type="button";
	cancel.value="Cancel";
	cancel.style.width="70px";
	cancel.style.margin="0px 10px 0px 10px";
	cancel.style.position="relative";
	cancel.style.verticalAlign = "middle";

	
	this.ok = ok;
	this.cancel = cancel;
	this.btnArea = btnArea
	this.msg = msg;
	this.form = form;
	this.xbox = xbox;
	this.titleText = titleText;
	this.title = title;
	this.dlg = dlg;
	this.ctnr = ctnr;
	this.screen = screen;
	this.block = block;
	
	if (xbox.addEventListener){
	    xbox.addEventListener("mouseover", function(){xbox.style.cursor="hand";}, false);
	    xbox.addEventListener("mouseout", function(){xbox.style.cursor="pointer";}, false);
	}else if (xbox.attachEvent){
	    xbox.attachEvent("onmouseover", function(){xbox.style.cursor="hand";});
	    xbox.attachEvent("onmouseout", function(){xbox.style.cursor="pointer";});
	}
	this.makeDragable();
    },
    
    makeDragable:function(){
	// only for gecko
	if(window.addEventListener){
	    this.onmousemovefn = function(e){
		    var objbox = window["jsMsgBox"];
		    
		    var newx = objbox.ox + (e.screenX-objbox.omx);
		    var newy = objbox.oy + (e.screenY-objbox.omy);

		    minx = -(window.innerWidth/2);
		    miny = -(window.innerHeight/2);
		    maxx = window.innerWidth/2-parseInt(objbox.dlg.style.width);
		    maxy = window.innerHeight/2-parseInt(objbox.dlg.style.height)

		    newx = newx>minx?newx:minx;
		    newy = newy>miny?newy:miny;

		    newx = newx<maxx?newx:maxx;
		    newy = newy<maxy?newy:maxy;

		    objbox.dlg.style.left = newx;
		    objbox.dlg.style.top = newy;
	    };
	    
	    this.titleText.addEventListener("mousedown", function(e){
		    var objbox = window["jsMsgBox"];
		    
		    objbox.ox = parseInt(objbox.dlg.style.left);
		    objbox.oy = parseInt(objbox.dlg.style.top);

		    objbox.omx = e.screenX;
		    objbox.omy = e.screenY;
		    objbox.titleText.addEventListener("mousemove", objbox.onmousemovefn, false);
		}, false);
	    
	    this.titleText.addEventListener("mouseup", function(e){
		    var objbox = window["jsMsgBox"];
		    objbox.titleText.removeEventListener("mousemove", objbox.onmousemovefn, false);
		}, false);
	}
	
    },
    
    hideDlg:function(){
	window["jsMsgBox"].ctnr.style.display="none";
    },

    reset:function()
    {
	// make controls
	this.makeDlg();
	// reset position
	this.dlg.style.top = "-100px";
	this.dlg.style.left = "-150px";
	
	if (this.xbox.removeEventListener){
	    if(this.okfn){
		this.ok.removeEventListener("click", this.okfn,false);
	    }
	    if(this.cancelfn){
		this.cancel.removeEventListener("click", this.cancelfn,false);
	    }
	    if(this.xboxfn){
		this.xbox.removeEventListener("click", this.xboxfn,false);
	    }
	}else if (xbox.attachEvent){
	    if(this.okfn){
		this.ok.detachEvent("onclick", this.okfn);
	    }
	    if(this.cancelfn){
		this.cancel.detachEvent("onclick", this.cancelfn);
	    }
	    if(this.xboxfn){
		this.xbox.detachEvent("onclick", this.xboxfn);
	    }
	}
    },
    setMsg:function(title, message)
    {
	this.titleText.textContent = title;
	this.msg.textContent = message;
	this.ctnr.style.display="block";
    },
    makefn:function(okfn, cancelfn, xboxfn)
    {
	var hideDlg = this.hideDlg;
	
	if(okfn){
	    this.okfn = function(){ hideDlg();okfn(); };
	}else{
	    this.okfn = hideDlg;
	}
	if(cancelfn){
	    this.cancelfn = function(){ hideDlg();cancelfn(); };
	}else{
	    this.cancelfn = hideDlg;
	}
	if(xboxfn){
	    this.xboxfn = function(){ hideDlg();xboxfn(); };
	}else{
	    this.xboxfn = hideDlg;
	}
    },
    setokfn:function(okfn)
    {
	if(document.addEventListener){
	    this.ok.addEventListener("click", okfn, false);
	}else{
	    this.ok.attachEvent("onclick", okfn);
	}
    },
    setcancelfn:function(cancelfn)
    {
	if(document.addEventListener){
	    this.cancel.addEventListener("click", cancelfn, false);
	}else{
	    this.cancel.attachEvent("onclick", cancelfn);
	}
    },
    setxboxfn:function (xboxfn)
    {
	if(document.addEventListener){
	    this.xbox.addEventListener("click", xboxfn, false);
	}else{
	    this.xbox.attachEvent("onclick", xboxfn);
	}
    },
    confirm:function(title, message, okfn, cancelfn, focusIndex)
    {
	this.reset();
	this.setMsg(title, message);
	this.makefn(okfn, cancelfn, cancelfn);
	this.setokfn(this.okfn);
	this.setcancelfn(this.cancelfn);
	this.setxboxfn(this.xboxfn);
	this.cancel.style.display="inline";
	if(focusIndex == 1){
	    this.ok.focus();
	}else{
	    this.cancel.focus();
	}
    },

    alert:function(title, message, okfn)
    {
	this.reset();
	this.setMsg(title, message);
	this.makefn(okfn, null, null);
	this.setokfn(this.okfn);
	this.setxboxfn(this.okfn);
	this.cancel.style.display="none";
	this.ok.focus();
    }
}
